
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_SGP30.h>
#include <Wire.h>

#define ONE_WIRE_BUS_1 3
#define ONE_WIRE_BUS_2 4

Adafruit_SGP30 sgp;
OneWire oneWire_in(ONE_WIRE_BUS_1);
OneWire oneWire_out(ONE_WIRE_BUS_2);
DallasTemperature sensor_inhouse(&oneWire_in);
DallasTemperature sensor_outhouse(&oneWire_out);
int button=2;
int counter = 0;
//int temp,temp2;
// 复位或上电后运行一次:
void setup() {
    //在这里加入初始化相关代码，只运行一次:
	Serial.begin(9600);
	Serial2.begin(9600);
	pinMode(2,INPUT);
	Serial.println("SGP30 test");
	pinMode(13,OUTPUT);
	digitalWrite(13,HIGH);
	if (! sgp.begin())
	{
	Serial.println("Sensor not found :(");
		while (1);
	}
	Serial.print("Found SGP30 serial #");
	Serial.print(sgp.serialnumber[0], HEX);
	Serial.print(sgp.serialnumber[1], HEX);
	Serial.println(sgp.serialnumber[2], HEX);
//    Serial.println("Dallas Temperature Control Library Demo - TwoPin_DS18B20");
	
    sensor_inhouse.begin();
    sensor_outhouse.begin();
}

//一直循环执行:
void loop() {
    // 在这里加入主要程序代码，重复执行:
	tempp();
	button=digitalRead(2);
	if(button==LOW)
		SendMMS("SGP30","18B20!!","19120556766");
}

uint32_t getAbsoluteHumidity(float temperature, float humidity) {
    // approximation formula from Sensirion SGP30 Driver Integration chapter 3.15
    const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3]
    const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3]
    return absoluteHumidityScaled;
}
void tempp()
{
	//Serial.print("Requesting temperatures...");
    sensor_inhouse.requestTemperatures();
    sensor_outhouse.requestTemperatures();
   // Serial.println(" done");

    Serial.print("Inside the car: ");
    float temp=sensor_inhouse.getTempCByIndex(0);
	Serial.print(temp); Serial.println(" °C");
	
	float temp2=sensor_outhouse.getTempCByIndex(0);
    Serial.print("Outside the car: ");
    Serial.print(temp2);Serial.println(" °C");
	if(temp>28)
		digitalWrite(13,LOW);
	else
		digitalWrite(13,HIGH);
	SGP30();
	
	
}
void SGP30() {
  // If you have a temperature / humidity sensor, you can set the absolute humidity to enable the humditiy compensation for the air quality signals
  //float temperature = 22.1; // [°C]
  //float humidity = 45.2; // [%RH]
  //sgp.setHumidity(getAbsoluteHumidity(temperature, humidity));

  if (! sgp.IAQmeasure()) {
    Serial.println("Measurement failed");
    return;
  }
  Serial.print("TVOC "); Serial.print(sgp.TVOC); Serial.print(" ppb\t");
  Serial.print("eCO2 "); Serial.print(sgp.eCO2); Serial.println(" ppm");
  delay(500);

  counter++;
  /*
  if (counter == 30) {
    counter = 0;

    uint16_t TVOC_base, eCO2_base;
    if (! sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
      Serial.println("Failed to get baseline readings");
      return;
    }
    Serial.print("****Baseline values: eCO2: 0x"); Serial.print(eCO2_base, HEX);
    Serial.print(" & TVOC: 0x"); Serial.println(TVOC_base, HEX);
  }
  */
}
char SendMMS(String Tit,String MSG,String PHnum)
{
	  String TEMPdata;
	  int titlen,MSGlen;
	  
      Serial.print("Tit===========");Serial.println(Tit);       //测试一下标题传送进来没有
      Serial.print("MSG===========");Serial.println(MSG);       //测试一下彩信内容传送进来没有
      Serial.print("PHnum===========");Serial.println(PHnum);   //测试一下手机号码传送进来没有
  
////////////////////////////////////////////////////////////////////////////////
      /*
	  Serial2.println("AT+CGATT=?");//因为彩信用HTTP协议发送，建立在GPRS之上所以必须查询网络附着情况AT+CMMSINIT
      delay(1000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
      Serial.print(TEMPdata);                                //打印出AT指令返回的信息，看是否OK   
      TEMPdata="";
      Serial.println("");
       */
      Serial2.println("AT+CMMSINIT");                     //初始化彩信功能
      delay(1000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
       
      Serial.print(TEMPdata);                                //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
       
      Serial2.println("AT+CMMSCURL=\"mmsc.monternet.com\"");//配置中国移动手机彩信中心地址
	  delay(2000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
       
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
       
      Serial2.println("AT+CMMSCID=1");//设置承载上下文ID 
        delay(1000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
       
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
       
      Serial2.println("AT+CMMSPROTO=\"10.0.0.172\",80");
	  delay(2000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
       
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
      Serial2.println("AT+CMMSSENDCFG=6,3,0,0,2,4,1,0");
        delay(1000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
       
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
	  delay(1000);
      ////////////////////////////////////////////////////////////激活承载
       
      Serial2.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
	  delay(2000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
         
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
       
      Serial2.println("AT+SAPBR=3,1,\"APN\",\"CMWAP\"");
        delay(1000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
       
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
       
      Serial2.println("AT+SAPBR=1,1");
        delay(1000);Serial.println("");
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
      delay(200);
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
       
      Serial2.println("AT+SAPBR=2,1");
        delay(1000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
       
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
       
      ////////////////////////////////////////////////////发送 
      delay(200);
      Serial2.println("AT+CMMSEDIT=1");
        delay(1000);Serial.println("");
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
       
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
		
        //Serial2.println("AT+CMMSDOWN=\"PIC\",7955,50000");//使用例程中的JPG格式图片，目前没有测试，所以注释掉了
      delay(200);
      titlen =Tit.length()+2;
      //Serial2.println("AT+CMMSDOWN=\"PIC\",8,50000");
      Serial2.print("AT+CMMSDOWN=\"PIC\",");
      Serial2.print( titlen );                               // 彩信标题的长度
      Serial.print( Tit );Serial.println( titlen );
      Serial2.println(",50000");
	  delay(1000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
         
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
      Serial2.println("FFFFFFFF");                //发送彩信标题
	  delay(1000);
      Serial.println(Tit);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
       
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
		
      MSGlen=MSG.length();
      //Serial2.println("AT+CMMSDOWN=\"TITLE\",270,50000");   //完整的设置内容的AT命令代码  270是长度可以根据内容长度更改
      Serial2.print("AT+CMMSDOWN=\"TITLE\",");
      Serial2.print(MSGlen);                           //彩信内容的长度
      Serial.print( MSG);Serial.println( MSGlen);
      Serial2.println(",50000");
        delay(1000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
      delay(200);
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
      Serial2.println(MSG);                    //彩信内容
	   //Serial.write(test,12);Serial.println("");
      Serial.println();
        delay(1000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        delay(1);
        }
      delay(200);
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
	  Serial2.println(Tit);   
		
	  delay(1000);	
	  Serial2.print("AT+CMMSDOWN=\"TEXT\",");
      Serial2.print(MSGlen);                           //彩信内容的长度
      Serial.print( MSG);Serial.println( MSGlen);
      Serial2.println(",50000");
        delay(1000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
      delay(200);
      Serial2.print(MSG);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
	  delay(1000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        delay(1);
        }
      delay(200);
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
       
	  //Serial2.println("AT+CMMSRECP=\"14403220001\"");          //完整的设置接收号码的AT命令代码
      Serial2.print("AT+CMMSRECP=");
      Serial2.print("\"");
      Serial2.print(PHnum);                     ////////////////目的手机号，务必保证手机号开通GPRS业务
      Serial2.println("\""); 
       delay(1000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
      delay(200);
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
		
	  delay(1000);
      Serial2.println("AT+CMMSCC=\"729769974@qq.com\"");        //发送至邮箱
	  delay(1000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
      delay(200);
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
       
      
      
	  Serial2.println("AT+CMMSVIEW");///////////////////////发送结束后关闭
	  delay(3000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
		Serial.println(TEMPdata); 
		TEMPdata="";
		delay(1000);
		 while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
		Serial.println(TEMPdata); 
		TEMPdata="";
		delay(1000);
		 while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
      delay(2000);
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
		
		delay(1000);
		
      Serial2.println("AT+CMMSSEND");///////////////////////发送结束后关闭
        delay(3000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
      delay(200);
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
       
      Serial2.println("AT+CMMSEDIT=0");///////////////退出配置模式
        delay(1000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
      delay(200);
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
      ///////////////////////////退出承载////////////////////////////////////////
      Serial2.println("AT+SAPBR=0,1");
        delay(1000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
      delay(200);
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
      /////////////////////////////////退出彩信功能/////////////////////////////
      Serial2.println("AT+CMMSTERM");
       delay(1000);
      while(Serial2.available())
        {
        TEMPdata+=char(Serial2.read());
        }
      delay(200);
      Serial.print(TEMPdata);                              //打印出AT指令返回的信息，看是否OK    
      TEMPdata="";
      Serial.println("");
       
//////////////////////////////////////////////////////////////
}